package com.ymatou.iapi.refund.testcase;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.optrade.resp.TradingServiceResp;
import com.ymatou.iapi.refund.parameter.CancelreturnapplyBean;
import com.ymatou.iapi.refund.parameter.CreateReturnBillBean;
import com.ymatou.iapi.refund.service.CancelreturnapplyCall;
import com.ymatou.iapi.refund.service.CreateReturnBillCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.RefundCallService;
import com.ymttest.business.service.YMTTrading.TradingCallService;
import com.ymttest.business.service.YMTTrading.TradingPlaceOrderCallService;
import com.ymttest.database.model.CS_ReturnApplyBill;
import com.ymttest.database.model.CS_ReturnApplyBillLog;
import com.ymttest.database.mongo.UserTokenWapper;
import com.ymttest.database.sqlwapper.CS_ReturnApplyBillLogWapper;
import com.ymttest.database.sqlwapper.CS_ReturnApplyBillWapper;
import com.ymttest.database.sqlwapper.CS_ReturnApplyPicturesWapper;
import com.ymttest.database.sqlwapper.CS_ReturnApplyProductsWapper;
/**
*撤销申请
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_cancelreturnapply{
	private static CancelreturnapplyBean bean;
	private static CancelreturnapplyCall call;
	private static CreateReturnBillBean createBean=new CreateReturnBillBean();
	private static CreateReturnBillCall createCall=new CreateReturnBillCall();
	private TradingServiceResp tradingServiceResp=new TradingServiceResp();
	private static CS_ReturnApplyBillWapper returnApplyBillWapper=new CS_ReturnApplyBillWapper();
	private static CS_ReturnApplyBillLogWapper returnApplyBillLog=new CS_ReturnApplyBillLogWapper();
	private UserTokenWapper userWapper;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("撤销申请");
	}

	@Before
	public void caseUp() throws Exception {
		bean=new CancelreturnapplyBean();
		call=new CancelreturnapplyCall();
		userWapper=new UserTokenWapper();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(call.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_cancelreturnapply_001() {
		Logger.start(true,"正例-撤销申请");
		try {
			//创建一个订单
			TradingPlaceOrderCallService tradingService=new TradingPlaceOrderCallService();
			RefundCallService service=new RefundCallService();
			TradingServiceResp tradingServiceResp=tradingService.placeOrderWithTwoProcutInOneOrder(false, false);
			createBean=service.setCreateReturnBillBean_AcceptOrder(tradingServiceResp,tradingService,false);
			createCall=service.callCreateReturnBill(createBean,createCall);
			
			bean.setReturnId(createCall.getCallData().getReturnBillList().get(0).getReturnId());
			bean.setUserId(createBean.getUserid());
			//根据userId 获取token
			String token=userWapper.getAccessTokenByUid(createBean.getUserid());
			bean.setAccessToken(token);
			bean.setcallback("");
			bean.setIsNew(true);
			
			call.setUrl(call.getUrl()+"?returnId="+bean.getReturnId()+"&userId="+bean.getUserId()+"&AccessToken="+token+"&callback=");
			
			//call.setData(bean);
			call.callService();			
			verifyReturn();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_cancelreturnapply_002() {
		Logger.start(false,"反例-撤销申请-退单不存在");
		try {
			//创建一个订单
			TradingPlaceOrderCallService tradingService=new TradingPlaceOrderCallService();
			RefundCallService service=new RefundCallService();
			TradingServiceResp tradingServiceResp=tradingService.placeOrderWithTwoProcutInOneOrder(false, false);
			createBean=service.setCreateReturnBillBean_AcceptOrder(tradingServiceResp,tradingService,false);
			createCall=service.callCreateReturnBill(createBean,createCall);
			
			bean.setReturnId("9999999");
			bean.setUserId(createBean.getUserid());
			//根据userId 获取token
			String token=userWapper.getAccessTokenByUid(createBean.getUserid());
			bean.setAccessToken(token);
			bean.setcallback("");
			bean.setIsNew(true);
			
			call.setUrl(call.getUrl()+"?returnId="+bean.getReturnId()+"&userId="+bean.getUserId()+"&AccessToken="+token+"&callback=");
			
			//call.setData(bean);
			call.callService();			
			Logger.verifyEquals(true,call.getMsg1().contains("退单不存在"), "验证Msg");
			Logger.verifyEquals("201",call.getStatus(), "验证Status");
			Logger.verifyEquals("1",call.getbCode(), "验证bCode");
			Logger.verifyEquals("false",call.getRetData(), "验证RetData");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_cancelreturnapply_003() {
		Logger.start(false,"反例-撤销申请-userId不存在");
		try {
			//创建一个订单
			TradingPlaceOrderCallService tradingService=new TradingPlaceOrderCallService();
			RefundCallService service=new RefundCallService();
			TradingServiceResp tradingServiceResp=tradingService.placeOrderWithTwoProcutInOneOrder(false, false);
			createBean=service.setCreateReturnBillBean_AcceptOrder(tradingServiceResp,tradingService,false);
			createCall=service.callCreateReturnBill(createBean,createCall);
			
			bean.setReturnId(createCall.getCallData().getReturnBillList().get(0).getReturnId());
			bean.setUserId(9999);
			//根据userId 获取token
			String token=userWapper.getAccessTokenByUid(createBean.getUserid());
			bean.setAccessToken(token);
			bean.setcallback("");
			bean.setIsNew(true);
			
			call.setUrl(call.getUrl()+"?returnId="+bean.getReturnId()+"&userId="+bean.getUserId()+"&AccessToken="+token+"&callback=");
			call.callService();			
			Logger.verifyEquals(true,call.getMsg1().contains("无效的用户"), "验证Msg");
			Logger.verifyEquals("500",call.getStatus(), "验证Status");
			Logger.verifyEquals("4",call.getbCode(), "验证bCode");
			Logger.verifyEquals("false",call.getRetData(), "验证RetData");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_cancelreturnapply_004() {
		Logger.start(false,"反例-撤销申请-token不存在");
		try {
			//创建一个订单
			TradingPlaceOrderCallService tradingService=new TradingPlaceOrderCallService();
			RefundCallService service=new RefundCallService();
			TradingServiceResp tradingServiceResp=tradingService.placeOrderWithTwoProcutInOneOrder(false, false);
			createBean=service.setCreateReturnBillBean_AcceptOrder(tradingServiceResp,tradingService,false);
			createCall=service.callCreateReturnBill(createBean,createCall);
			
			bean.setReturnId(createCall.getCallData().getReturnBillList().get(0).getReturnId());
		//	bean.setUserId(createBean.getUserid());
			//根据userId 获取token
			//String token=userWapper.getAccessTokenByUid(createBean.getUserid());
			bean.setAccessToken("999999");
			bean.setcallback("");
			bean.setIsNew(true);
			
			call.setUrl(call.getUrl()+"?returnId="+bean.getReturnId()+"&userId="+bean.getUserId()+"&AccessToken="+bean.getAccessToken()+"&callback=");
			call.callService();			
			Logger.verifyEquals(true,call.getMsg1().contains("authtoken failed"), "验证Msg");
			Logger.verifyEquals("201",call.getStatus(), "验证Status");
			Logger.verifyEquals("1",call.getbCode(), "验证bCode");
			Logger.verifyEquals("false",call.getRetData(), "验证RetData");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_cancelreturnapply_005() {
		Logger.start(false,"反例-撤销申请-退单不支持撤销");
		try {
			//创建一个订单
			TradingPlaceOrderCallService tradingService=new TradingPlaceOrderCallService();
			RefundCallService service=new RefundCallService();
			TradingServiceResp tradingServiceResp=tradingService.placeOrderWithTwoProcutInOneOrder(false, false);
			createBean=service.setCreateReturnBillBean_AcceptOrder(tradingServiceResp,tradingService,false);
			createCall=service.callCreateReturnBill(createBean,createCall);
			
			bean.setReturnId(createCall.getCallData().getReturnBillList().get(0).getReturnId());
			bean.setUserId(createBean.getUserid());
			//根据userId 获取token
			String token=userWapper.getAccessTokenByUid(createBean.getUserid());
			bean.setAccessToken(token);
			bean.setcallback("");
			bean.setIsNew(true);
			
			//CS_ReturnApplyBill 表中的action 更改action=4，表示洋管家审核拒绝
			returnApplyBillWapper.updateActionByReturnId(bean.getReturnId(), 4);
			
			call.setUrl(call.getUrl()+"?returnId="+bean.getReturnId()+"&userId="+bean.getUserId()+"&AccessToken="+bean.getAccessToken()+"&callback=");
			call.callService();			
			Logger.verifyEquals(true,call.getMsg1().contains("退单不支持撤消"), "验证Msg");
			Logger.verifyEquals("500",call.getStatus(), "验证Status");
			Logger.verifyEquals("7",call.getbCode(), "验证bCode");
			Logger.verifyEquals("false",call.getRetData(), "验证RetData");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_cancelreturnapply_006() {
		Logger.start(false,"反例-撤销申请-退单不支持撤销-Action=11:审核通过 && type=0：仅退款");
		try {
			//创建一个订单
			TradingPlaceOrderCallService tradingService=new TradingPlaceOrderCallService();
			RefundCallService service=new RefundCallService();
			TradingServiceResp tradingServiceResp=tradingService.placeOrderWithTwoProcutInOneOrder(false, false);
			createBean=service.setCreateReturnBillBean_AcceptOrder(tradingServiceResp,tradingService,false);
			createCall=service.callCreateReturnBill(createBean,createCall);
			
			bean.setReturnId(createCall.getCallData().getReturnBillList().get(0).getReturnId());
			bean.setUserId(createBean.getUserid());
			//根据userId 获取token
			String token=userWapper.getAccessTokenByUid(createBean.getUserid());
			bean.setAccessToken(token);
			bean.setcallback("");
			bean.setIsNew(true);
			
			//CS_ReturnApplyBill 表中的action 更改action=4，表示洋管家审核拒绝
			returnApplyBillWapper.updateActionByReturnId(bean.getReturnId(), 11);
			returnApplyBillWapper.updateTypeByReturnId(bean.getReturnId(), 0);
			
			call.setUrl(call.getUrl()+"?returnId="+bean.getReturnId()+"&userId="+bean.getUserId()+"&AccessToken="+bean.getAccessToken()+"&callback=");
			call.callService();			
			Logger.verifyEquals(true,call.getMsg1().contains("退单不支持撤消"), "验证Msg");
			Logger.verifyEquals("500",call.getStatus(), "验证Status");
			Logger.verifyEquals("7",call.getbCode(), "验证bCode");
			Logger.verifyEquals("false",call.getRetData(), "验证RetData");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	private void verifyReturn()
	{
		Logger.verifyEquals("",call.getMsg1(), "验证Msg");
		Logger.verifyEquals("200",call.getStatus(), "验证Status");
		Logger.verifyEquals("0",call.getbCode(), "验证bCode");
		Logger.verifyEquals("true",call.getRetData(), "验证RetData");
		
	   CS_ReturnApplyBill returnApplyBill=returnApplyBillWapper.selectByApplyBillId(bean.getReturnId()).get(0);
	   Logger.verifyEquals(returnApplyBill.getAction(), 0, "验证CS_ReturnApplyBill表Action是否正确，Action=0 表示已撤消");
	   
	   CS_ReturnApplyBillLog logModel=returnApplyBillLog.selectByApplyBillId(bean.getReturnId()).get(1);
	   Logger.verifyEquals(returnApplyBill.getReturnApplyBillId(), logModel.getReturnApplyBillId(), "验证CS_ReturnApplyBill表ReturnApplyBillId是否正确");
	   
	   Logger.verifyEquals( logModel.getReturnApplyBillId(), returnApplyBill.getReturnApplyBillId(),"验证CS_ReturnApplyBillLog表ReturnApplyBillId是否正确");
	   Logger.verifyEquals(logModel.getOperatorType(), 2, "验证CS_ReturnApplyBillLog表OperatorType是否正确");
	   Logger.verifyEquals(logModel.getType(), returnApplyBill.getType(), "验证CS_ReturnApplyBillLog表Type是否正确");
	   Logger.verifyEquals(logModel.getReturnAmount(), tradingServiceResp.getPayableAmount(), "验证CS_ReturnApplyBillLog表ReturnAmount是否正确");
	   Logger.verifyEquals(logModel.getReturnBillAction(), returnApplyBill.getAction(), "验证CS_ReturnApplyBillLog表ReturnBillAction是否正确");
	   Logger.verifyEquals(logModel.getAccepterLoginId(), returnApplyBill.getAccepterLoginId(), "验证CS_ReturnApplyBillLog表accepterLoginId是否正确");
	   Logger.verifyEquals(logModel.getAccepterType(), returnApplyBill.getAccepterType(), "验证CS_ReturnApplyBillLog表accepterType是否正确");
	}
}